from flask import Flask, url_for, request
import click

app = Flask(__name__)

@app.route('/')
def index():
    return '<h1>Hello Flask World!</h1>'


# 为视图函数绑定多个URL
@app.route('/hi')
@app.route('/hello')
def say_hello():
    return '<h1>Hello Flask!</h1>'


# 动态URL，带有参数的URL
@app.route('/greet', defaults={'name': 'Tester'}) # 设置name参数的默认值
@app.route('/greet/<name>')
def greet(name):
    title = '<h1>Hello %s!</h1>' % name 
    result = '<h3>path: %s</h3>' % request.path
    result += '<h3>full_path: %s</h3>' % request.full_path
    result += '<h3>host: %s</h3>' % request.host
    result += '<h3>host_url: %s</h3>' % request.host_url
    result += '<h3>base_url: %s</h3>' % request.base_url
    result += '<h3>url: %s</h3>' % request.url
    result += '<h3>url_root: %s</h3>' % request.url_root
    return title + result


# 测试 url_for 函数
@app.route('/urlfor')
def test_url_for():
    url1 = url_for('index')
    url2 = url_for('greet', name='Jack')
    return '<h3>%s , %s </h3>' % (url1, url2)


# flask自定义命令, 使用时 flask cmd-hello
@app.cli.command()
def cmd_hello():
    click.echo('Hello click!!!')


